package rabbitmq.wrok;

import com.rabbitmq.client.*;
import rabbitmq.constant.Constants;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer2 {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 创建连接
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(Constants.HOST);
        factory.setPort(Constants.PORT);
        factory.setUsername(Constants.USER_NAME);
        factory.setPassword(Constants.PASSWORD);
        factory.setVirtualHost(Constants.VIRTUAL);
        // 建立建立
        Connection connection = factory.newConnection();
        // 建立通信
        Channel channel = connection.createChannel();
        // 声明队列
        channel.queueDeclare(Constants.WORK_QUEUE_MAME,true,false,false,null);
        DefaultConsumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("接收的消息: " + new String(body));
            }
        };
        // 消费消息
        channel.basicConsume(Constants.WORK_QUEUE_MAME,true,consumer);
        // 释放资源
//        channel.close();
//        connection.close();
    }
}
